package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TProductCategory;
import com.woniu.car.domain.TSpec;
import com.woniu.car.domain.TSpecValue;
import com.woniu.car.dto.QueryGoodsAllSpec;
import com.woniu.car.dto.TestDto;
import com.woniu.car.dto.ValuesDto;
import com.woniu.car.mapper.TProductCategoryMapper;
import com.woniu.car.mapper.TSpecMapper;
import com.woniu.car.mapper.TSpecValueMapper;
import com.woniu.car.param.AddSpecParam;
import com.woniu.car.service.TSpecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 规格组 服务实现类
 * </p>
 *
 * @author chestnuts
 * @since 2021-06-21
 */
@Slf4j
@Service
public class TSpecServiceImpl extends ServiceImpl<TSpecMapper, TSpec> implements TSpecService {

    @Resource
    private TSpecMapper specMapper;


    @Resource
    private TSpecValueMapper specValueMapper;


    @Resource
    private TProductCategoryMapper productCategoryMapper;

    @Override
    public ResponseEntity addSpec(AddSpecParam addSpecParam){

        //为一个分类添加新的规格组

        TProductCategory one = productCategoryMapper.selectById(addSpecParam.getCategoryId());
        log.info("校验,{}",one);
        if(one == null){
            return new ResponseEntity().setCode(500).setMsg("该分类不存在,请勿添加");
        }
        QueryWrapper<TSpec> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_name",addSpecParam.getSpecName());
        TSpec tSpec1 = specMapper.selectOne(wrapper);
        log.info("校验该规格组是否存在,{}",tSpec1);
        if(tSpec1!=null&&tSpec1.getCategoryId()==addSpecParam.getCategoryId()){
            log.info("校验该规格组是否存在");
            return new ResponseEntity().setCode(500).setMsg("该规格组已存在请勿重复添加");
        }

        TSpec tSpec = new TSpec();
        BeanCopyUtils.copyProperties(addSpecParam,tSpec);

        tSpec.setCreateNo("56832165489532");
        tSpec.setCreateTime(Long.toString(System.currentTimeMillis()));
        int insert = specMapper.insert(tSpec);
        if(insert>1){
            QueryWrapper<TSpec> wrapper1 = new QueryWrapper<>();
            wrapper.eq("spec_name",addSpecParam.getSpecName());
            tSpec = specMapper.selectOne(wrapper);
        }
        log.info("sda,{}",tSpec);
        for(String i: addSpecParam.getSpecValues()){
            TSpecValue tSpecValue = new TSpecValue();
            tSpecValue.setSpecValue(i);
            tSpecValue.setSpecId(tSpec.getSpecId());
            tSpecValue.setCreateNo("56832165489532");
            tSpecValue.setCreateTime(Long.toString(System.currentTimeMillis()));
            specValueMapper.insert(tSpecValue);
        }

        return new ResponseEntity().setMsg("添加成功").setObject(tSpec1).setCode(200);
    }

    @Override
    public ResponseEntity getAllSpecByGategory(Integer gategoryId) {
        //先看这个类别的等级 如果等级！=2 不是基础的分类 不展示 其拥有的规格组

        TProductCategory tProductCategory = productCategoryMapper.selectById(gategoryId);
        if(tProductCategory==null)return new ResponseEntity().setMsg("该分类不存在,请重新输入").setCode(503);

        //查出一个list（规格名 规格值）
        log.info("====================");
        List<QueryGoodsAllSpec> speces = specMapper.getSpeces(gategoryId);
        if(speces==null||speces.size()==0){
            return new ResponseEntity().setMsg("该分类没有规格组").setCode(501);
        }
        log.info("asdsadasd,{}",speces.size());
        //准备一个容器
        Integer p =0;
        List<TestDto> dtos = new ArrayList<>();
        log.info("aaa,{}",speces);
        int o=speces.get(0).getSpecId();
        TestDto testDto = new TestDto();
        List<String> av=new ArrayList<String>();
        for(int i=0;i<speces.size();i++){

        }
        for(int i=0;i<=speces.size();i++){
            if(i==speces.size()){
                testDto.setValues(av);
                dtos.add(testDto);
                break;
            }
           if(o==speces.get(i).getSpecId()){
               testDto.setSpecId(speces.get(i).getSpecId());
               testDto.setSpecName(speces.get(i).getSpecName());
               av.add(speces.get(i).getSpecValue());
           }else{
               testDto.setValues(av);
               dtos.add(testDto);
               av=new ArrayList<String>();
               testDto = new TestDto();
               testDto.setSpecId(speces.get(i).getSpecId());
               testDto.setSpecName(speces.get(i).getSpecName());
               av.add(speces.get(i).getSpecValue());
               o=speces.get(i).getSpecId();
           }
        }
        return new ResponseEntity().setCode(200).setMsg("成功获取").setObject(dtos);
    }


}
